package com.ynet.middleground.user.service.impl;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.AddEmployeeReq;
import com.ynet.middleground.user.bean.AddOrganRoleRelationshipReq;
import com.ynet.middleground.user.bean.AddUserRoleRelationReq;
import com.ynet.middleground.user.bean.InitializeDataEnterpriseBasicReq;
import com.ynet.middleground.user.bean.InitializeDataOrganRoleRelationshipReq;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.common.SystemConstant;
import com.ynet.middleground.user.model.InitializeDataModel;
import com.ynet.middleground.user.service.InitializeDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("initialize")
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8", MediaType.TEXT_XML + "; " + "charset=UTF-8"})
@Api(value = "初始化数据临时用的服务", tags = {"初始化数据临时用的服务"}, hidden = true)
@Service(timeout = 6000)
public class InitializeDataImpl implements InitializeDataService, GenericService {

    @Autowired
    InitializeDataModel initializeDataModel;


    @POST
    @Path("relationship")
    @Override
    public ServiceResult<String> initializeDataOrganRoleRelationship(InitializeDataOrganRoleRelationshipReq requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            result = initializeDataModel.initializeDataOrganRoleRelationship(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }
    
    @POST
    @Path("testaddemployee")
    @Override
    public ServiceResult<String> testAddEmployee(AddEmployeeReq requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        Integer result = null;
        try {
            result = initializeDataModel.testAddEmployee(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result.toString());
        return resultService;
    }


    @POST
    @Path("initialize-enterprise-basic")
    @ApiOperation(value = "初始化用户数据", notes = "初始化用户数据")
    @Override
    public ServiceResult<String> initializeDataEnterpriseBasic(List<InitializeDataEnterpriseBasicReq> requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            initializeDataModel.initializeDataEnterpriseBasic(requestObject);
            result = SystemConstant.ERROR_MSG_SUCCESS;
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }
    

    @POST
    @Path("initialize-enterprise-rolerelation")
    @ApiOperation(value = "初始化企业员工角色关系", notes = "初始化企业员工角色关系")
    @Override
    public ServiceResult<String> initializeEnterpriseUserRoleRelation(List<AddUserRoleRelationReq> requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            initializeDataModel.initializeEnterpriseUserRoleRelation(requestObject);
            result = SystemConstant.ERROR_MSG_SUCCESS;
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }
    

    @POST
    @Path("initialize-employee-rolerelation")
    @ApiOperation(value = "初始化港内员工角色关系", notes = "初始化港内员工角色关系")
    @Override
    public ServiceResult<String> initializeEmployeeRolerelation(List<AddOrganRoleRelationshipReq> requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            initializeDataModel.initializeEmployeeRolerelation(requestObject);
            result = SystemConstant.ERROR_MSG_SUCCESS;
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;
    }
}
